package com.jia.interviewPro;

/**
 * 面试题 03.01. 三合一
 */
public class Pro0301 {
    int[] arr;
    int[] index;
    int stackSize;
    public Pro0301(int stackSize) {
        arr = new int[stackSize * 3];
        this.stackSize = stackSize;
        index = new int[]{0, 1, 2};
    }

    public void push(int stackNum, int value) {
        if(index[stackNum] >= this.stackSize * 3) {
            return;
        }
        arr[index[stackNum]] = value;
        index[stackNum] += 3;
    }

    public int pop(int stackNum) {
        if(isEmpty(stackNum)) {
            return - 1;
        }
        index[stackNum] -= 3;
        return arr[index[stackNum]];
    }

    public int peek(int stackNum) {
        if(isEmpty(stackNum)) {
            return -1;
        }
        return arr[index[stackNum] - 3];
    }

    public boolean isEmpty(int stackNum) {
        return index[stackNum] < 3;
    }
}
